<?php
/*
 * 进云智能生成平台
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
 */

defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return['setting']=$this->plugin_setting('basic');
$uniacid=$_SESSION['uniacid'];
if($_RQ['op']=='main'){
	//查询管家
	$list=pdo_getall('jy_smallquan_manager',array('uniacid'=>$uniacid,'status'=>1));
	
	$return['list']=$list;
	$return['params']=array('keyword'=>'');
	$fuwu=pdo_getall('jy_smallquan_apart_assess',array('uniacid'=>$_SESSION['uniacid'],'status'=>'1'));
	$star5=pdo_count('jy_smallquan_apart_assess',array('uniacid'=>$_SESSION['uniacid'],'status'=>'1','star'=>'5'));
	$star4=pdo_count('jy_smallquan_apart_assess',array('uniacid'=>$_SESSION['uniacid'],'status'=>'1','star'=>'4'));
	$star3=pdo_count('jy_smallquan_apart_assess',array('uniacid'=>$_SESSION['uniacid'],'status'=>'1','star'=>'3'));
	$star2=pdo_count('jy_smallquan_apart_assess',array('uniacid'=>$_SESSION['uniacid'],'status'=>'1','star'=>'2'));
	$star1=pdo_count('jy_smallquan_apart_assess',array('uniacid'=>$_SESSION['uniacid'],'status'=>'1','star'=>'1'));
	$fuwu_count=count($fuwu);
	if($fuwu_count>0){
		$fuwu['star5']=$star5/$fuwu_count*100;
		$fuwu['star4']=$star4/$fuwu_count*100;
		$fuwu['star3']=$star3/$fuwu_count*100;
		$fuwu['star2']=$star2/$fuwu_count*100;
		$fuwu['star1']=$star1/$fuwu_count*100;
	}else{
		$fuwu['star5']=$fuwu['star4']=$fuwu['star3']=$fuwu['star2']=$fuwu['star1']=0;
	}
	//获取用户当月评价
	$month_assess=pdo_get('jy_smallquan_apart_assess',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'time'=>date('Y-m')));
	//评价平均分
	$sql = "SELECT SUM(star) FROM " . tablename('jy_smallquan_apart_assess') . " WHERE uniacid=:uniacid";
	$sum = pdo_fetch($sql,array(":uniacid" => $_SESSION['uniacid']));	
	$star_sum = $sum['SUM(star)'];
	$star_fen=$fuwu_count>0&&$star_sum>0?number_format($star_sum/$fuwu_count,1):4.6;
	$return['fuwu']=$fuwu;
	$return['post']=array('star'=>$month_assess['star']);
	$return['month']=date('m');
	$return['month_assess']=$month_assess;
	$return['star_fen']=$star_fen;
	$return['fuwu_count']=$fuwu_count;
	exi($return);
}
if($_RQ['op']=='getlist'){
	if(!empty($_RQ['keyword'])){
		$params['name like']='%'.trim($_RQ['keyword']).'%';
		$return['params']['keyword']=$_RQ['keyword'];
	}
	$params['status']=1;
	$params['uniacid']=$uniacid;
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','name','avatar','mobile','bid','mid','score','status','createtime');
	$list=pdo_getslice('jy_smallquan_manager',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$assess1=pdo_getall('jy_smallquan_manager_assess',array('uniacid'=>$_SESSION['uniacid'],'managerid'=>$item['id']),'','','createtime desc limit 5');
			$assess=pdo_getall('jy_smallquan_manager_assess',array('uniacid'=>$_SESSION['uniacid'],'managerid'=>$item['id']));
			$item['msgnum']=count($assess);
			if($assess){
				foreach($assess1 as &$row){
					$zhuhu=pdo_get('jy_smallquan_household',array('id'=>$row['zid']));
					$zhuhu['avatar']=pdo_getcolumn('core_members',array('id'=>$row['mid']),'avatar');
					$row['zhuhu']=$zhuhu;
					$row['zhuhu']=$zhuhu;
					$row['createtime']=date('Y-m-d',$item['createtime']);
				}
				$item['assess1']=$assess1;
				$item['assess']=$assess;
				$item['new']=$assess['0'];
			}
		}
	}
	$return['list']=$list;
	exi($return);
	
}
if($_RQ['op']=='setmsg'){
	$post=get_postdata();
	if($post['isgood']==''){
		exi('请选择评价类型','error');
	}
	if($post['remark']==''){
		exi('请输入评价内容','error');
	}
	$zid=pdo_getcolumn('jy_smallquan_household',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'id');
	$insert=array(
		'uniacid'=>$_SESSION['uniacid'],
		'mid'=>$_SESSION['uid'],
		'zid'=>$zid,
		'managerid'=>$post['pid'],
		'star'=>$post['star'],
		'isgood'=>$post['star']>=4?1:0,
		'content'=>$post['remark'],
		'createtime'=>TIMESTAMP,
	);
	pdo_insert('jy_smallquan_manager_assess',$insert);
	exi('评价成功!');
	
}
if($_RQ['op']=='pingjia'){
	$star=$_RQ['star'];
	$managerid=$_RQ['id'];
	if(!$managerid){
		exi('暂无管家，请联系管理员添加！','error');
	}
	$guanjia=pdo_get('jy_smallquan_manager',array('uniacid'=>$_SESSION['uniacid'],'id'=>$managerid));
	$pingjianum=pdo_count('jy_smallquan_manager_assess',array('uniacid'=>$_SESSION['uniacid'],'managerid'=>$guanjia['id']));
	$pinjia_good=pdo_count('jy_smallquan_manager_assess',array('uniacid'=>$_SESSION['uniacid'],'managerid'=>$guanjia['id'],'isgood'=>'1'));
	$guanjia['manyidu']=number_format($pinjia_good/$pingjianum,0);
	$guanjia['pingjianum']=$pingjianum;
	$return['guanjia']=$guanjia;
	$return['post']=array('star'=>$star,'managerid'=>$managerid);
	exi($return);
}
		
if($_RQ['op']=='star'){
	$post=get_postdata();
	$zid=pdo_getcolumn('jy_smallquan_household',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'id');
	$insert=array(
		'uniacid'=>$_SESSION['uniacid'],
		'mid'=>$_SESSION['uid'],
		'zid'=>$zid,
		'star'=>$post['star'],
		'time'=>date('Y-m'),
		'status'=>1,
		'createtime'=>TIMESTAMP,
	);
	pdo_insert('jy_smallquan_apart_assess',$insert);
	exi('评价成功!');
}
if($_RQ['op']=='submit'){
	$post=get_postdata();
	//print_r($post);exit;
	$zid=pdo_getcolumn('jy_smallquan_household',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'id');
	if($post['star']>2){
		$isgood=1;
	}else{
		$isgood=0;
	}
	$insert=array(
		'uniacid'=>$_SESSION['uniacid'],
		'mid'=>$_SESSION['uid'],
		'zid'=>$zid,
		'managerid'=>$post['managerid'],
		'isgood'=>$isgood,
		'content'=>$post['content'],
		'label'=>$post['label'],
		'status'=>1,
		'createtime'=>TIMESTAMP,
	);
	pdo_insert('jy_smallquan_manager_assess',$insert);
	exi('评价成功!');
}		


